package com.qidian.hw.service.impl;

import com.qidian.hw.dao.NewsDAO;
import com.qidian.hw.dao.impl.NewsDAOImpl;
import com.qidian.hw.pojo.News;
import com.qidian.hw.service.NewsService;
import com.qidian.hw.tools.dto.Pager;


import java.util.ArrayList;

/**
 * @Author ꒰ঌ(李ᆺ琛)໒꒱
 * @Date 2022/8/19 20:05
 * @Version 1.8
 */
public class NewsServiceImpl implements NewsService {
    private NewsDAO newsDAO = new NewsDAOImpl ();

    @Override
    public int saveNews(News news) {
        return newsDAO.saveNews (news);
    }

    @Override
    public int deleteNews(long newsId) {
        return newsDAO.deleteNews (newsId);
    }

    @Override
    public int updateNews(News news) {
        return newsDAO.updateNews (news);
    }

    @Override
    public ArrayList<News> listNews() {
        return newsDAO.listNews ();
    }

    @Override
    public News queryNewsById(long newsId) {
        return newsDAO.queryNewsById (newsId);
    }

    @Override
    public Pager queryByPage(int page, int size, String title) {
        Pager pager = new Pager ();
        pager.setPage (page);
        pager.setSize (size);
        // 查询总条数
        int total = newsDAO.count (title);
        pager.setTotal (total);
        // 计算最大页码
        int maxPage = (int) Math.ceil (total * 1.0 / size);
        pager.setMaxPage (maxPage);
        // 查询数据列表
        ArrayList rows = newsDAO.queryByPage (page, size, title);
        pager.setRows (rows);
        return pager;
    }

    @Override
    public int deleteBatch(String[] ids) {
        return newsDAO.deleteBatch (ids);
    }
}
